package service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import dao.MemberDao;
import dao.impl.MemberDaoImpl;
import dto.SelectCondition;
import entity.Member;
import service.MemberService;
import vo.MemberVO;

public class MemberServiceImpl implements MemberService {

	MemberDao dao = new MemberDaoImpl();
	
	@Override
	public List<MemberVO> getByPage(int pageSize, int pg) {
		// 一个集合的member
		List<Member> memberList = dao.selectByPage(pageSize,pg);
		// 倒腾
		if (memberList.isEmpty()) {
			return Collections.emptyList();
		}else {
			List<MemberVO> mvo = new ArrayList<>();
			for (Member m : memberList) {
				MemberVO vo = new MemberVO();
				vo.setMemberId(m.getMemberId());
				vo.setMemberNick(m.getMemberNick());
				vo.setMemberPhone(m.getMemberPhone());
				vo.setMoney(m.getMoney());
				vo.setOriginName(m.getOrigin().getOriginName());
				vo.setTypeName(m.getType().getTypeName());
				vo.setRegistTime(m.getRegistTime());
				vo.setStat(m.getStat()==0?"正常":"黑名单");
				mvo.add(vo);
			}
			return mvo;
		}
	}

	@Override
	public MemberVO getById(int id) {
		Member m = dao.selectById(id);
		if (m == null) {
			return null;
		}else {
			MemberVO vo = new MemberVO();
			vo.setMemberId(m.getMemberId());
			vo.setMemberNick(m.getMemberNick());
			vo.setMemberPhone(m.getMemberPhone());
			vo.setMoney(m.getMoney());
			vo.setOriginName(m.getOrigin().getOriginName());
			vo.setTypeName(m.getType().getTypeName());
			vo.setRegistTime(m.getRegistTime());
			vo.setStat(m.getStat()==0?"正常":"黑名单");
			return vo;
		}
	}

	@Override
	public MemberVO getByPhone(String phone) {
		Member m = dao.selectByPhone(phone);
		if (m == null) {
			return null;
		}else {
			MemberVO vo = new MemberVO();
			vo.setMemberId(m.getMemberId());
			vo.setMemberNick(m.getMemberNick());
			vo.setMemberPhone(m.getMemberPhone());
			vo.setMoney(m.getMoney());
			vo.setOriginName(m.getOrigin().getOriginName());
			vo.setTypeName(m.getType().getTypeName());
			vo.setRegistTime(m.getRegistTime());
			vo.setStat(m.getStat()==0?"正常":"黑名单");
			return vo;
		}
	}

	@Override
	public List<MemberVO> getByCondition(SelectCondition cond) {
		// 一个集合的member
		List<Member> memberList = dao.selectByCondition(cond);
		// 倒腾
		if (memberList.isEmpty()) {
			return Collections.emptyList();
		}else {
			List<MemberVO> mvo = new ArrayList<>();
			for (Member m : memberList) {
				MemberVO vo = new MemberVO();
				vo.setMemberId(m.getMemberId());
				vo.setMemberNick(m.getMemberNick());
				vo.setMemberPhone(m.getMemberPhone());
				vo.setMoney(m.getMoney());
				vo.setOriginName(m.getOrigin().getOriginName());
				vo.setTypeName(m.getType().getTypeName());
				vo.setRegistTime(m.getRegistTime());
				vo.setStat(m.getStat()==0?"正常":"黑名单");
				mvo.add(vo);
			}
			return mvo;
		}
	}

	@Override
	public boolean save(Member member) {
		return dao.insert(member);
	}

	@Override
	public boolean update(Member member) {
		return dao.update(member);
	}

	@Override
	public boolean removeByIds(List<Integer> ids) {
		return dao.deleteByIds(ids);
	}

	@Override
	public int getTotalCount() {
		return dao.selectCount();
	}

	@Override
	public int getCountByCondition(SelectCondition cond) {
		return dao.selectCountByCondition(cond);
	}
}
